<?php

	// +----------------------------------------------------------------------
	// | 用户模型验证器
	// +----------------------------------------------------------------------
	// | Copyright (c) 2015-2024 http://www.yicmf.com, All rights reserved.
	// +----------------------------------------------------------------------
	// | Author: 微尘 <yicmf@qq.com>
	// +----------------------------------------------------------------------

	namespace app\validate\ucenter;

	use app\common\validate\Base;
	use app\model\ucenter\User as UserModel;
	use think\facade\Env;
	use think\facade\Lang;
	use think\facade\Session;

	class User extends Base
	{

		/* 用户模型自动验证 */
		protected $rule = [
			'username|登录账号' => [
//                'mobile',
				'length' => '5,28',
				'graph',
			],
			'email' => [
				'email',
				'checkEmail' => 'mobile',
				'unique' => 'user',
			],
			'short_code|个性域名' => [
				'alphaDash',
				'unique' => 'user',
			],
			'nickname|用户昵称' => [
				'length' => '2,28',
//				'unique' => 'user',
				'chsDash',
			],
			'mobile|手机号' => [
//				'checkMobile' => 'mobile',
				'mobile'
			],
			'auth_name|真实姓名' => 'chs',
			'password|密码' => [
				'length' => '5,28',
				'confirm'
			],
			'captcha' => [
				'checkCaptcha'
			]
		];
		protected $message = [
			'captcha.require' => '请填写验证码',
			'username.require' => '登陆账号必填',
			'username.length' => '登录账号长度不符合要求',
			'username.password' => '密码长度不符合要求',
		];
		// edit 验证场景定义
		public function sceneProfile()
		{
			return $this->remove('captcha','checkCaptcha');
		}
		public function sceneUpdate()
		{
			return $this->remove('captcha','checkCaptcha');
		}
	// edit 验证场景定义
		public function sceneInstall()
		{
			return $this->only(['username', 'password', 'email'])->remove('username', 'unique');
		}
		public function sceneForget()
		{
			return $this->only(['email','password'])->remove('email',['checkEmail','unique']);
		}
		public function sceneSetting()
		{
			return $this->only(['nickname', 'password', 'email'])->append('nickname', 'require');
		}

		// edit 验证场景定义
		public function sceneOauth()
		{
			return $this->only(['nickname']);
		}

		public function sceneChangepassword()
		{
			return $this->only(['password','confirm_password']);
		}

		public function sceneLogin()
		{
//			return $this->only(['username', 'password', 'captcha'])
			return $this->only(['username', 'password', 'captcha'])
				->remove('password', 'confirm');
		}

		public function sceneRegister()
		{
			return $this->only(['mobile', 'username', 'password', 'nickname', 'email']);
		}


		/**
		 * 检测用户名是不是被禁止注册.
		 * @param $value
		 * @param $rule
		 * @param $data
		 * @return bool|string
		 * @author  : 微尘 <yicmf@qq.com>
		 */
		protected function checkAccount($value, $rule, $data)
		{
            return ;
//			if (empty($data['id']) && UserModel::where('username', $value)->where('status', '>', -2)->find())
//			{
//				return '当前账户已经注册';
//			}elseif (!empty($data['id']) && UserModel::where('username', $value)->where('id','<>',$data['id'])->where('status', '>', -2)->find())
//			{
//				return '当前账户已经注册';
//			}else{
//				return true;
//			}
		}


		/**
		 * 检测用户名是不是被禁止注册.
		 * @param $value
		 * @param $rule
		 * @param $data
		 * @return bool|string
		 * @author  : 微尘 <yicmf@qq.com>
		 */
		protected function checkCaptcha($value, $rule, $data)
		{
			if (!captcha_check($value))
			{
				return '验证码错误';
			}else{
				return true;
			}
		}

		/**
		 * 检测用户名是不是被禁止注册.
		 * @param $value
		 * @param $rule
		 * @param $data
		 * @return bool
		 * @author  : 微尘 <yicmf@qq.com>
		 * @datetime: 2019/2/16 18:44
		 */
		protected function checkEmail($value, $rule, $data)
		{
			// 是否唯一
			if (empty($data['id']) && UserModel::where('email', $value)->where('status', '>', -2)->find())
			{
				return '当前账户已经注册';
			}elseif (!empty($data['id']) && UserModel::where('email', $value)->where('id','<>',$data['id'])->where('status', '>', -2)->find())
			{
				return '当前账户已经注册';
			}else{
				return true;
			}
		}

		/**
		 * 检测用户名是不是被禁止注册.
		 * @param $value
		 * @param $rule
		 * @param $data
		 * @return bool
		 * @author  : 微尘 <yicmf@qq.com>
		 */
		protected function checkMobile($value, $rule, $data)
		{
			$user = UserModel::where('mobile', $value)
//				->where('status', '>', -2)
				->when(isset($data['id']), function ($query) use ($data) {
					// 满足条件后执行
					$query->where('id', '<>', $data['id']);
				})->find();
			// 是否唯一
            dump($rule);
            dump($data);
			if ($user) {
				return '当前手机号已经注册';
			}
			return true;
		}

		/**
		 * 检测邮箱是不是被禁止注册.
		 * @param string $value 邮箱
		 * @return bool ture - 未禁用，false - 禁止注册
		 */
		protected function checkDenyEmail($value, $rule, $data)
		{
			return true; //TODO: 暂不限制，下一个版本完善
		}

		/**
		 * 检测手机是不是被禁止注册.
		 * @param string $value 手机
		 * @return bool ture - 未禁用，false - 禁止注册
		 */
		protected function checkDenyMobile($value, $rule, $data)
		{
			return true; //TODO: 暂不限制，下一个版本完善
		}
	}
